﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes {
    import Engine.Events.*;
    import flash.display.*;
    import flash.geom.*;
    import Engine.Helpers.*;
    import Transactions.*;
    import Classes.util.*;
    import __AS3__.vec.*;
    import Classes.effects.*;
    import Display.*;
    import Modules.franchise.display.*;
    import Classes.actions.*;

    public class Headquarter extends MapResource {

        protected const TYPE_NAME:String = "headquarter";
        protected const STATE_BASE:String = "base";
        protected const SCAFFOLD_BASE_COUNT:int = 4;
        protected const MIN_BUILT_FLOOR_COUNT:int = 1;
        protected const IMAGE_NAME_FLOOR:String = "floor";
        protected const IMAGE_NAME_ROOFTOP:String = "rooftop";
        protected const IMAGE_NAME_PENTHOUSE:String = "penthouse";
        protected const IMAGE_NAME_BILLBOARD:String = "billboard";

        protected var m_contentImageNames:Array;
        protected var m_isContentAssetsLoaded:Boolean = false;
        protected var m_itemImageHash:Object;
        protected var m_bitmapDataHash:Object;
        protected var m_originalBitmapData:BitmapData;
        protected var m_canvasBitmapData:BitmapData;
        protected var m_itemName:String;
        protected var m_heightLimit:Number;
        protected var m_builtFloorCount:Number = 0;
        protected var m_scaffoldEffect:ScaffoldEffect;
        protected var m_isBuildingNewFloor:Boolean = false;

        public function Headquarter(_arg1:String){
            this.m_contentImageNames = [this.IMAGE_NAME_FLOOR, this.IMAGE_NAME_ROOFTOP, this.IMAGE_NAME_PENTHOUSE, this.IMAGE_NAME_BILLBOARD];
            this.m_itemImageHash = {};
            this.m_bitmapDataHash = {};
            super(_arg1);
            this.m_itemName = _arg1;
            m_objectType = WorldObjectTypes.HEADQUARTER;
            m_typeName = this.TYPE_NAME;
            this.m_heightLimit = this.getHeightLimit();
            this.m_builtFloorCount = this.MIN_BUILT_FLOOR_COUNT;
            setState(this.STATE_BASE);
        }
        public function get heightLimit():Number{
            return (this.m_heightLimit);
        }
        override public function loadObject(_arg1:Object):void{
            super.loadObject(_arg1);
            this.m_builtFloorCount = ((_arg1.builtFloorCount) || (0));
            this.m_builtFloorCount = Math.min(this.m_builtFloorCount, this.m_heightLimit);
            if (this.m_builtFloorCount < this.MIN_BUILT_FLOOR_COUNT){
                this.m_builtFloorCount = this.MIN_BUILT_FLOOR_COUNT;
            };
        }
        override public function isSellable():Boolean{
            return (true);
        }
        override public function getItemFriendlyName():String{
            var _local1 = "";
            if (m_item){
                _local1 = Global.franchiseManager.getHeadquartersFriendlyName(m_item.name);
            };
            return (_local1);
        }
        override public function drawDisplayObject():void{
            super.drawDisplayObject();
            if (((m_content) && (!(this.m_isContentAssetsLoaded)))){
                this.m_originalBitmapData = this.contentBitmap.bitmapData.clone();
                this.loadContentAssets();
            } else {
                if (this.m_canvasBitmapData){
                    this.setContentBitmap();
                    this.updateScaffold();
                };
            };
        }
        override public function cleanUp():void{
            var _local1:ItemImage;
            super.cleanUp();
            this.cleanUpScaffold();
            for each (_local1 in this.m_itemImageHash) {
                _local1.removeEventListener(LoaderEvent.LOADED, this.itemImageLoadedHandler);
            };
            this.m_itemImageHash = null;
            this.m_bitmapDataHash = null;
            this.m_contentImageNames = null;
            this.m_originalBitmapData = null;
        }
        protected function cleanUpScaffold():void{
            if (this.m_scaffoldEffect){
                this.m_scaffoldEffect.cleanUp();
                this.m_scaffoldEffect = null;
            };
        }
        private function getHeadquartersName():String{
            var _local1:String = Global.franchiseManager.getFranchiseTypeFromHeadquarters(this.m_itemName);
            var _local2:String = Global.franchiseManager.worldOwnerModel.getFranchiseName(_local1);
            var _local3:String = ZLoc.t("Items", "headquarters", {item:_local2});
            return (_local3);
        }
        override public function getToolTipHeader():String{
            var _local1:String = this.getHeadquartersName();
            return (_local1);
        }
        override public function getToolTipAction():String{
            var _local1 = "";
            if (this.m_isBuildingNewFloor){
                _local1 = "";
            } else {
                if (((this.isScaffoldNeeded) && (!(Global.isVisiting())))){
                    _local1 = ZLoc.t("Main", "BuildNewFloor");
                } else {
                    _local1 = super.getToolTipAction();
                };
            };
            return (_local1);
        }
        override public function onPlayAction():void{
            var _local1:String;
            var _local2:FranchiseMenu;
            super.onPlayAction();
            if (((Global.isVisiting()) || (((actionBar) && (actionBar.visible))))){
                return;
            };
            if (!this.isScaffoldNeeded){
                _local1 = Global.franchiseManager.getFranchiseTypeFromHeadquarters(this.m_itemName);
                _local2 = UI.displayNewFranchise();
                _local2.switchBusiness(_local1);
            } else {
                this.buildNewFloor();
            };
        }
        override public function updateAlpha():void{
            if (((((m_actionBar) && (m_actionBar.visible))) || (this.m_isBuildingNewFloor))){
                this.alpha = 1;
            } else {
                super.updateAlpha();
            };
        }
        public function drawContent():void{
            var _local26:BitmapData;
            var _local27:Rectangle;
            var _local1:BitmapData = this.m_originalBitmapData.clone();
            var _local2:Number = _local1.height;
            var _local3:Number = _local1.width;
            var _local4:BitmapData = this.getImageBitmapData(this.IMAGE_NAME_FLOOR);
            var _local5:ItemImage = this.getContentItemImage(this.IMAGE_NAME_FLOOR);
            var _local6:Number = _local4.height;
            var _local7:Number = _local4.width;
            var _local8:Number = _local5.offsetX;
            var _local9:Number = _local5.offsetY;
            var _local10:Number = _local5.forcedHeight;
            var _local11:String = (this.isPenthouse) ? this.IMAGE_NAME_PENTHOUSE : this.IMAGE_NAME_ROOFTOP;
            var _local12:BitmapData = this.getImageBitmapData(_local11);
            var _local13:ItemImage = this.getContentItemImage(_local11);
            var _local14:Number = _local12.height;
            var _local15:Number = _local12.width;
            var _local16:Number = _local13.offsetX;
            var _local17:Number = _local13.offsetY;
            var _local18:BitmapData = this.getImageBitmapData(this.IMAGE_NAME_BILLBOARD);
            var _local19:ItemImage = this.getContentItemImage(this.IMAGE_NAME_BILLBOARD);
            var _local20:Number = _local18.height;
            var _local21:Number = _local18.width;
            var _local22:Number = _local19.offsetX;
            var _local23:Number = _local19.offsetY;
            var _local24:Number = (((_local2 - _local9) + (_local10 * (this.m_builtFloorCount - 1))) - _local17);
            var _local25:Number = Math.max(_local3, _local7, _local15);
            this.m_canvasBitmapData = new BitmapData(_local25, _local24, true, 0);
            var _local28:Point = new Point();
            var _local29:Number = 0;
            var _local30:Number = (_local24 - _local2);
            _local26 = _local1;
            _local27 = _local1.rect;
            _local28.x = _local29;
            _local28.y = _local30;
            this.m_canvasBitmapData.copyPixels(_local26, _local27, _local28, null, null, true);
            var _local31:Number = (_local29 + _local8);
            var _local32:Number = (_local30 + _local9);
            _local26 = _local4;
            _local27 = _local4.rect;
            _local28.x = _local31;
            _local28.y = _local32;
            var _local33:int = this.m_builtFloorCount;
            while (_local33--) {
                this.m_canvasBitmapData.copyPixels(_local26, _local27, _local28, null, null, true);
                _local28.y = (_local28.y - _local10);
            };
            var _local34:Number = (_local31 + _local16);
            var _local35:Number = ((_local28.y + _local10) + _local17);
            _local26 = _local12;
            _local27 = _local12.rect;
            _local28.x = _local34;
            _local28.y = _local35;
            this.m_canvasBitmapData.copyPixels(_local26, _local27, _local28, null, null, true);
            var _local36:Number = (_local34 + _local22);
            var _local37:Number = (_local35 + _local23);
            _local26 = _local18;
            _local27 = _local18.rect;
            _local28.x = _local36;
            _local28.y = _local37;
            this.m_canvasBitmapData.copyPixels(_local26, _local27, _local28, null, null, true);
            this.setContentBitmap();
        }
        private function setContentBitmap():void{
            this.contentBitmap.bitmapData = (m_maskBitmap.bitmapData = this.m_canvasBitmapData.clone());
            var _local1:Number = (-(displayObjectOffsetY) / displayObject.scaleY);
            var _local2:Number = (_local1 - this.m_canvasBitmapData.height);
            this.contentBitmap.y = _local2;
        }
        private function getImageBitmapData(_arg1:String):BitmapData{
            return (this.m_bitmapDataHash[_arg1]);
        }
        private function getHeightLimit():Number{
            return (parseFloat(getItem().xml.heightLimit));
        }
        private function getContentItemImage(_arg1:String):ItemImage{
            return (this.m_itemImageHash[_arg1]);
        }
        private function get contentBitmap():Bitmap{
            var _local3:Vector.<DisplayObject>;
            var _local4:DisplayObject;
            var _local1:Bitmap = (m_content as Bitmap);
            var _local2:DisplayObjectContainer = (m_content as DisplayObjectContainer);
            if ((((_local1 == null)) && (_local2))){
                _local3 = DisplayObjectContainerUtil.getChildren(_local2);
                for each (_local4 in _local3) {
                    if ((_local4 is Bitmap)){
                        _local1 = (_local4 as Bitmap);
                        break;
                    };
                };
            };
            return (_local1);
        }
        public function get floorCount():int{
            var _local1:int = Global.franchiseManager.getHeadquartersHeight(this.m_itemName, ((m_itemOwner) || (Global.world.ownerId)));
            return (_local1);
        }
        private function get isPenthouse():Boolean{
            var _local1 = (this.floorCount >= this.heightLimit);
            return (_local1);
        }
        private function loadContentAssets():void{
            var imageName:* = null;
            var imageXml:* = null;
            var itemImage:* = null;
            var rawImageXml:* = m_item.rawImageXml;
            for each (imageName in this.m_contentImageNames) {
                imageXml = rawImageXml.(@name == imageName)[0];
                itemImage = new ItemImage(imageXml);
                this.m_itemImageHash[imageName] = itemImage;
                itemImage.addEventListener(LoaderEvent.LOADED, this.itemImageLoadedHandler, false, 0, true);
                itemImage.load();
            };
        }
        private function updateScaffold():void{
            var _local1:int;
            var _local2:int;
            var _local3:int;
            if (this.isScaffoldNeeded){
                _local1 = (this.SCAFFOLD_BASE_COUNT + this.m_builtFloorCount);
                _local2 = _local1;
                _local3 = (_local1 + 1);
                if (this.m_isBuildingNewFloor){
                    _local2 = ((this.SCAFFOLD_BASE_COUNT + this.floorCount) + 1);
                    _local3 = _local2;
                    this.cleanUpScaffold();
                };
                if (this.m_scaffoldEffect){
                    this.m_scaffoldEffect.startStage = _local1;
                    this.m_scaffoldEffect.endStage = _local2;
                    this.m_scaffoldEffect.maxStage = _local3;
                    this.m_scaffoldEffect.reattach();
                } else {
                    this.m_scaffoldEffect = new ScaffoldEffect(this, _local1, _local2, _local3);
                };
            } else {
                this.cleanUpScaffold();
            };
        }
        private function get isScaffoldNeeded():Boolean{
            var _local1:Boolean = Boolean((((this.m_builtFloorCount < this.floorCount)) && ((this.m_builtFloorCount <= this.m_heightLimit))));
            return (_local1);
        }
        private function buildNewFloor():void{
            var _mapResource:* = null;
            var workerData:* = null;
            this.m_isBuildingNewFloor = true;
            this.updateScaffold();
            this.m_builtFloorCount = this.floorCount;
            var workerDataArray:* = [];
            var i:* = 4;
            while ((i = (i - 1)), i) {
                workerData = {};
                workerData.pos = new Vector3(0, 0);
                workerData.isTempWorker = false;
                workerDataArray[workerDataArray.length] = workerData;
            };
            Global.world.citySim.npcManager.addConstructionWorkers(this, workerDataArray);
            _mapResource = this;
            m_actionQueue.removeAllStates();
            m_actionQueue.addActions(new ActionProgressBar(null, this, ZLoc.t("Main", "Building"), 5), new ActionFn(this, function ():void{
                drawContent();
                m_isBuildingNewFloor = false;
                updateScaffold();
                Global.world.citySim.npcManager.removeAllConstructionWorkers(_mapResource);
            }), new ActionPause(this, 1), new ActionFn(this, function ():void{
                Global.franchiseManager.headquarterFloorBuildCompleted(m_itemName);
            }));
            var tBuildFloorHeadquarter:* = new TBuildFloorHeadquarter(this);
            GameTransactionManager.addTransaction(tBuildFloorHeadquarter);
        }
        private function itemImageLoadedHandler(_arg1:LoaderEvent):void{
            var _local2:ItemImage;
            var _local3:ItemImageInstance;
            var _local4:int;
            var _local5:BitmapData;
            if (!this.m_isContentAssetsLoaded){
                _local2 = (_arg1.target as ItemImage);
                _local3 = _local2.getInstance();
                if (!_local3){
                    return;
                };
                _local2.removeEventListener(LoaderEvent.LOADED, this.itemImageLoadedHandler);
                if (!this.m_bitmapDataHash[_local2.name]){
                    this.m_bitmapDataHash[_local2.name] = (_local3.image as Bitmap).bitmapData;
                    _local4 = 0;
                    for each (_local5 in this.m_bitmapDataHash) {
                        _local4 = (_local4 + 1);
                    };
                    this.m_isContentAssetsLoaded = Boolean((_local4 >= this.m_contentImageNames.length));
                    if (this.m_isContentAssetsLoaded){
                        this.drawContent();
                        this.updateScaffold();
                    };
                };
            };
        }
        override public function onSell():void{
            super.onSell();
        }
        public function get builtFloorCount():int{
            return (this.m_builtFloorCount);
        }

    }
}//package Classes 
